package com.itheima.tanhua.dubbo.mongo.api.impl;

import com.itheima.tanhua.api.RecommendApi;
import com.itheima.tanhua.mongo.RecommendUser;
import com.itheima.tanhua.mongo.UserLike;
import com.itheima.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.BsonDouble;
import org.bson.BsonInt64;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lff
 * @date 2022/3/26
 */

@DubboService
//@Service
public class RecommendApiImpl implements RecommendApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分页查询所有佳人的id
     *
     * @param toUserid
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult queryToUserWithPages(Long toUserid, Integer page, Integer pagesize) {
        //创建criteria
        //"score").gte(new BsonDouble(80)).lte(new BsonDouble(100));//查询分数在80-100分之间的数据
        Criteria criteria = Criteria.where("toUserId").is(toUserid)
                .andOperator(Criteria.where("score").gte(new BsonDouble(80)).lte(new BsonDouble(100)));

        Query score = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")));//查询所有的记录，按照score降序

        List<RecommendUser> recommendUsers = mongoTemplate.find(score, RecommendUser.class);
        PageResult pageResult = new PageResult();
        //返回
        pageResult.setItems(recommendUsers);
        return pageResult;
    }

    /**
     * 根据UserId和toUserId查询recommentd
     *
     * @param toUserId
     * @param bestUserId
     * @return
     */
    @Override
    public RecommendUser findRecommendUserOne(Long toUserId, Long bestUserId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId).and("userId").is(bestUserId);
        Query query = Query.query(criteria);
        RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);
        return one;
    }

    /**
     * 根据userId获取列表
     *
     * @param id
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> queryRandomList(Long id, int count) {
        //查询用户喜欢表
        List<UserLike> likeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(id)), UserLike.class);

        //抽取id 喜欢的用户id
        List<Long> likeIds = likeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        //查询mongo
        Criteria criteria = Criteria.where("userId").nin(likeIds).and("toUserId").is(id);
        TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(
                RecommendUser.class,
                Aggregation.match(criteria), //以这个条件作匹配
                Aggregation.sample(count) //查询十条
        );
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        //获取返回值
        return aggregate.getMappedResults();
    }


    /**
     * @param userId 当前登录用户id
     * @param ids    需要查询的id集合
     * @return
     */
    @Override
    public List<RecommendUser> findRecommendUserList(Long userId, List<Long> ids) {
        Criteria criteria = Criteria.where("userId").in(ids)
                .and("toUserId").is(userId);
        Query query = Query.query(criteria);
        return mongoTemplate.find(query, RecommendUser.class);
    }

}
